package com.itheima.test;

import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManger {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();

        lo:
        while (true) {
            System.out.println("-------------学生信息管理系统----------------");
            System.out.println("1 增加学生");
            System.out.println("2 修改学生");
            System.out.println("3 删除学生");
            System.out.println("4 查找学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择：");

            String choice = sc.next();


            switch (choice) {
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    updateStudent(list);
                    break;
                case "3":
                    deleteStudent(list);
                    break;
                case "4":
                    queryStudent(list);
                    break;
                case "5":
                    System.out.println("退出,感谢您的使用！");
                    break lo;
                default:
                    System.out.println("您输入的信息有误，请检查！");
                    break;
            }
        }


    }

    private static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生学号：");
        String updateSid = sc.next();

        int index = getIndex(list, updateSid);

        if (index == -1) {
            System.out.println("您输入的信息有误，请重新输入！");
        } else {
            System.out.println("请输入新的学生学号：");
            String sid = sc.next();
            System.out.println("请输入新的学生姓名：");
            String name = sc.next();
            System.out.println("请输入新的学生年龄：");
            int age = sc.nextInt();
            System.out.println("请输入新的学生生日：");
            String birthday = sc.next();

            Student stu = new Student(sid, name, age, birthday);

            list.set(index, stu);
            System.out.println("修改成功!");
        }
    }

    private static void deleteStudent(ArrayList<Student> list) {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除学生的学号：");
        String sid = sc.next();

        int index = getIndex(list, sid);

        if (index == -1) {
            System.out.println("查无信息，请重新输入！");
        } else {
            list.remove(index);
            System.out.println("删除成功!");
        }
    }

    private static int getIndex(ArrayList<Student> list, String sid) {

        int index = -1;

        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String id = stu.getSid();
            if (id.equals(sid)) {
                index = i;
            }
        }
        return index;
    }

    private static void queryStudent(ArrayList<Student> list) {

        if (list.size() == 0) {
            System.out.println("无信息，请添加后查找!");
            return;
        }

        System.out.println("学号\t\t\t姓名\t\t年龄\t\t\t生日");

        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge()
                    + "\t\t" + stu.getBirthday());
        }
    }

    public static void addStudent(ArrayList<Student> list) {

        Scanner sc = new Scanner(System.in);
        String sid;

        while (true) {

            System.out.println("请输入学生学号：");
            sid = sc.next();

            int index = getIndex(list, sid);
            if (index == -1) {
                //-1 说明学号不存在，可以使用
                break;
            }
        }

        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();

        Student stu = new Student(sid, name, age, birthday);

        list.add(stu);
        System.out.println("添加成功!");
    }
}
